<?php

namespace app\teacher\controller\learn;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\learn\LearnSkRecord;
use app\common\service\learn\LearnHearingService;

class LearnHearingRecord  extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 开始-听力测试
     * */
    public function startHearing()
    {
        $courseId = $this->request->post('courseId');
        $skRecordId = $this->request->post('skRecordId');
        $studentId = $this->request->post('studentId');
        if (!$courseId){
            $this->fail('课程ID不能为空');
        }
        // 获取当前用户信息
        $accountInfo = $this->auth->getUserInfo();
        $accountId = $accountInfo['id'];
        $accountType = AccountTypeEnum::TRAINER; // 默认为教练
        $accountOrganizationId = $accountInfo['organizationId'];
        $tutorTrainerId = null;

        // 如果有上课记录ID，则表示为学生测试
        if ($skRecordId) {
            if (!$studentId) {
                $this->fail('学生ID不可为空');
            }

            $LearnSkRecordModel = new LearnSkRecord();
            $accountId = (string)$studentId;
            $accountType = AccountTypeEnum::STUDENT;
            $accountOrganizationId = $LearnSkRecordModel->getStudentOrgIdById($skRecordId);
            $tutorTrainerId = $accountInfo['id'];
        }

        $LearnHearingService = new LearnHearingService();
        $result = $LearnHearingService->startHearing(
            $courseId,
            $accountId,
            $accountType,
            $accountOrganizationId,
            $skRecordId,
            $tutorTrainerId
        );
        if (!$result){
            $this->fail($LearnHearingService->getError());
        }
        $this->ok('提交成功', $result);
    }

    /*
     * 提交-听力测试
     * */
    public function submitVocabulary()
    {
        // 获取请求中的JSON数据
        $data = $this->request->post();

        // 验证必要参数
        $recordId = isset($data['recordId']) ? $data['recordId'] : 0;
        $answerList = isset($data['answerList']) ? $data['answerList'] : [];

        if (!$recordId) {
            $this->fail('测试ID不能为空');
        }

        // 确定trainerId和studentId
        $trainerId = $this->auth->id;

        // 构造提交数据
        $submitData = [
            'recordId' => $recordId,
            'answerList' => $answerList
        ];

        $LearnHearingService = new LearnHearingService();
        $result = $LearnHearingService->submitAnswer(
            null,
            $trainerId,
            $submitData
        );
        if (!$result){
            $this->fail($LearnHearingService->getError());
        }
        $this->ok('提交成功', $result);
    }

    /*
     * 查询-测评评估报告
     * */
    public function getDetail()
    {
        $id = $this->request->get('id');
        $LearnHearingService = new LearnHearingService();
        $result = $LearnHearingService->getDetail($id);
        $this->ok('操作成功', $result);
    }
}